from django.core.management.base import BaseCommand,CommandError
import os
from django.conf import settings

class Command(BaseCommand):
    help = 'Acquire information about the num of picture in the album'

    def add_arguments(self, parser):
        parser.add_argument("--total-pic-num",action = "count", help=("The total number of uploaded pictures."),)
        parser.add_argument("--total-page-num",action = "count",help=("The total number of pages with 10 or fewer."),)

    def handle(self, *args, **options):
        if options["total_pic_num"] and options["total_page_num"]:
            raise CommandError("The --total-pic-num and --total-page-num can't be used together")

        pic_list = os.listdir(os.path.join(settings.BASE_DIR,"ablum","static"))
        pic_num=len(pic_list)

        # 查询照片总的数量
        if options["total_pic_num"]:
            self.stdout.write("There totally are %d pictures"%pic_num)

        # 查询照片有多少页
        if options["total_page_num"]:
            surplus = pic_num % 10
            if  surplus == 0:
                page_num = pic_num // 10
                self.stdout.write("There exactly are %d pages"%page_num)
            else:
                page_num = pic_num // 10 + 1
                self.stdout.write("There  are %d pages, and the last page contains %d pictures."%(page_num,surplus))

        # 默认查询照片总数
        if not (options["total_pic_num"] or options["total_page_num"]):
            self.stdout.write("There totally are %d pictures"%pic_num)